package vn.vfriends.vbox.rest;

import java.util.List;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.interceptor.Interceptors;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import vn.vfriends.vbox.facade.RoleFacade;
import vn.vfriends.vbox.interceptor.LoggingInterceptor;
import vn.vfriends.vbox.model.Role;

/**
 *
 * @author tuancn
 */
@Interceptors(LoggingInterceptor.class)
@Stateless
@Path("roles")
public class RoleService {
    
    @Inject 
    private RoleFacade roleFacade;

    @POST
    @Consumes({"application/xml", "application/json"})
    public Role createRole(Role role) {
        roleFacade.create(role);
        return role;
    }

    @PUT
    @Consumes({"application/xml", "application/json"})
    public Role editRole(Role role) {
        roleFacade.edit(role);
        return role;
    }

    @DELETE
    @Path("{id: \\d+}")
    public void remove(@PathParam("id") Integer id) {
        roleFacade.remove(roleFacade.find(id));
    }

    @GET
    @Path("{id: \\d+}")
    @Produces({"application/xml", "application/json"})
    public Role find(@PathParam("id") Integer id) {
        return roleFacade.find(id);
    }
    
    @GET
    @Path("code/{code}")
    @Produces({"application/xml", "application/json"})
    public Role findbyCode(@PathParam("code") String code) {
        return roleFacade.findbyCode(code);
    }
    
    @GET
    @Path("name/{name}")
    @Produces({"application/xml", "application/json"})
    public Role findbyName(@PathParam("name") String name) {
        return roleFacade.findbyName(name);
    }

    @GET
    @Produces({"application/xml", "application/json"})
    public List<Role> findAll() {
        return roleFacade.findAll();
    }

    @GET
    @Path("{from}/{to}")
    @Produces({"application/xml", "application/json"})
    public List<Role> findRange(@PathParam("from") Integer from, @PathParam("to") Integer to) {
        return roleFacade.findRange(new int[]{from, to});
    }

    @GET
    @Path("count")
    public int count() {
        return roleFacade.count();
    }
}
